#!/usr/bin/env python
import gtk
import gtk.glade
import sys,os,re,string

class NewPass:

	def do_validate_pass(self,update=True):
		mytext=self.passwd1.get_text()
		self.passwd=mytext

		goodpass=True
		
		#Min Length 
		if len(mytext)>=8:
			if update:
				self.set_tick(self.ct1)
		else:
			if update:
				self.set_cross(self.ct1)
			goodpass=False

		#lower
		if self.regex_l.search(mytext) is None:
			if update:
				self.set_cross(self.ct2)
			goodpass=False
			
		else:
			if update:
				self.set_tick(self.ct2)

		#upper
		if self.regex_u.search(mytext) is None:
			if update:
				self.set_cross(self.ct3)
			goodpass=False
		else:
			if update:
				self.set_tick(self.ct3)

		#number
		if self.regex_d.search(mytext) is None:
			if update:
				self.set_cross(self.ct4)
			goodpass=False
		else:
			if update:
				self.set_tick(self.ct4)

		#symbol
		if self.regex_p.search(mytext) is None:
			if update:
				self.set_cross(self.ct5)
			goodpass=False
		else:
			if update:
				self.set_tick(self.ct5)


		return goodpass
	
	def on_passwd1_key_press_event(self,widget,event):
		if event.keyval==65293: #Is enter key
			goodpass=self.do_validate_pass(update=False)
			if goodpass:
				self.passwd2.grab_focus()

	def on_passwd2_key_press_event(self,widget,event):
		if event.keyval==65293: #Is enter key
			if self.passwd2.get_text()==self.passwd1.get_text():
				goodpass=self.do_validate_pass(update=False)
				if goodpass:
					self.on_ok_clicked()


	def on_ok_clicked(self,widget=None):
		self.success=True
		if self.passwd2.get_text()==self.passwd1.get_text():
			goodpass=self.do_validate_pass(update=False)
			if goodpass:
				self.password=self.passwd2.get_text()
				self.window.hide_all()
				gtk.main_quit()
		
	def on_cancel_clicked(self,widget=None):
		md = gtk.MessageDialog(self.window,
				       gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_QUESTION, 
				       gtk.BUTTONS_YES_NO, "Are you sure to quit?")
		a=md.run()
		md.destroy()

		if a==gtk.RESPONSE_YES:
			self.window.hide_all()
			gtk.main_quit()
			self.success=False

		
	def set_tick(self,gtkimg):
		gtkimg.set_from_icon_name("gtk-yes",4)

	def set_cross(self,gtkimg):
		gtkimg.set_from_icon_name("gtk-no",4)

	def on_passwd2_changed(self,widget):
		mytext=self.passwd2.get_text()
		if mytext==self.passwd1.get_text():
			self.set_tick(self.ct6)
			if self.do_validate_pass(update=False):
				self.ok.set_sensitive(True)
		else:
			self.set_cross(self.ct6)
			self.ok.set_sensitive(False)
		
	def on_passwd1_changed(self,widget):
		goodpass=self.do_validate_pass(update=True)
		if self.passwd1.get_text()==self.passwd2.get_text() and goodpass:
			self.set_tick(self.ct6)
			self.ok.set_sensitive(True)
		else:
			self.set_cross(self.ct6)
			self.ok.set_sensitive(False)
		
	def __init__(self,prompttext):

		self.success=False
		
		#regex
		self.regex_p = re.compile('[%s]' % re.escape(string.punctuation))
		self.regex_l = re.compile('[%s]' % re.escape(string.lowercase))
		self.regex_u = re.compile('[%s]' % re.escape(string.uppercase))
		self.regex_d = re.compile('[%s]' % re.escape(string.digits))

		#Set the Glade file
		self.gladefile = sys.path[0]+"/newpasswordwindow.glade"  
	        self.wTree = gtk.glade.XML(self.gladefile) 
		
		#Get the Main Window, and connect the "destroy" event
		self.window = self.wTree.get_widget("mainwindow")

		dic = {
			"on_passwd1_key_press_event" : self.on_passwd1_key_press_event,			
			"on_passwd2_key_press_event" : self.on_passwd2_key_press_event,			
			"on_passwd1_changed" : self.on_passwd1_changed,
			"on_passwd2_changed" : self.on_passwd2_changed,
			"on_ok_clicked":self.on_ok_clicked,
			"on_cancel_clicked":self.on_cancel_clicked,
			}

		self.wTree.signal_autoconnect(dic)


		if (self.window):
			self.window.connect("destroy", self.on_mainwindow_destroy)
			
		self.ct1=self.wTree.get_widget("ct1")
		self.ct2=self.wTree.get_widget("ct2")
		self.ct3=self.wTree.get_widget("ct3")
		self.ct4=self.wTree.get_widget("ct4")
		self.ct5=self.wTree.get_widget("ct5")
		self.ct6=self.wTree.get_widget("ct6")

		self.passwd1=self.wTree.get_widget("passwd1")
		self.passwd2=self.wTree.get_widget("passwd2")

		self.ok=self.wTree.get_widget("ok")

		if not prompttext is None:
			self.textmsg=self.wTree.get_widget("textmsg")
			self.textmsg.get_buffer().set_text(prompttext)

	def on_mainwindow_destroy(self,widget):
		self.success=False
		self.window.hide_all()
		gtk.main_quit()


def doNewPass(prompttext=None):
	np = NewPass(prompttext)
	gtk.main()
	if np.success:
		return (True,np.password)
	return (False,None)
	
if __name__ == "__main__":

	if len(sys.argv)>1:
		prompttext=sys.argv[1]
	else:
		prompttext=None
	
	
	(status,password)=doNewPass(prompttext)
	
	if status:
		print password
		sys.exit(0)

	sys.exit(-1)
	
		
